const app = getApp();

Page({
  data: {
    userInfo: null,
    nickname: '',
    isLoggedIn: false
  },

  onLoad: function() {
    this.checkLoginStatus();
  },

  onShow: function() {
    this.checkLoginStatus();
  },

  checkLoginStatus: function() {
    const userInfo = wx.getStorageSync('userInfo');
    console.log('当前用户信息:', userInfo);
    
    if (userInfo) {
      this.setData({
        userInfo: userInfo,
        nickname: userInfo.nickname || userInfo.username,
        isLoggedIn: true
      });
    } else {
      this.setData({
        userInfo: null,
        nickname: '',
        isLoggedIn: false
      });
    }
  },

  onLogin: function() {
    wx.navigateTo({
      url: '/pages/login/login'
    });
  },

  onMyFavorites: function() {
    if (!this.data.isLoggedIn) {
      this.onLogin();
      return;
    }
    wx.navigateTo({
      url: '/pages/favorite/favorite'
    });
  },

  onMyOrders: function() {
    if (!this.data.isLoggedIn) {
      this.onLogin();
      return;
    }
    wx.showToast({
      title: '功能开发中',
      icon: 'none'
    });
  },

  onMyReviews: function() {
    if (!this.data.isLoggedIn) {
      this.onLogin();
      return;
    }
    wx.showToast({
      title: '功能开发中',
      icon: 'none'
    });
  },

  onSettings: function() {
    wx.navigateTo({
      url: '/pages/settings/settings'
    });
  },

  onAbout: function() {
    wx.showModal({
      title: '关于我们',
      content: '这是一个旅游推荐小程序，为您提供优质的旅游景点推荐服务。',
      showCancel: false
    });
  },

  onLogout: function() {
    wx.showModal({
      title: '提示',
      content: '确定要退出登录吗？',
      success: (res) => {
        if (res.confirm) {
          wx.removeStorageSync('userInfo');
          this.setData({
            userInfo: null,
            nickname: '',
            isLoggedIn: false
          });
          wx.showToast({
            title: '已退出登录',
            icon: 'success'
          });
        }
      }
    });
  }
}); 